package proguard.classfile.util;

import java.util.List;

public class ClassUtil {
    private static final String EMPTY_STRING = "";

    public ClassUtil() {
    }

    public static void checkMagicNumber(int var0) throws UnsupportedOperationException {
        if (var0 != -889275714) {
            throw new UnsupportedOperationException("Invalid magic number [" + Integer.toHexString(var0) + "] in class");
        }
    }

    public static int internalClassVersion(int var0, int var1) {
        return var0 << 16 | var1;
    }

    public static int internalMajorClassVersion(int var0) {
        return var0 >>> 16;
    }

    public static int internalMinorClassVersion(int var0) {
        return var0 & '\uffff';
    }

    public static int internalClassVersion(String var0) {
        return !var0.equals("1.0") && !var0.equals("1.1") ? (var0.equals("1.2") ? 3014656 : (var0.equals("1.3") ? 3080192 : (var0.equals("1.4") ? 3145728 : (!var0.equals("5") && !var0.equals("1.5") ? (!var0.equals("6") && !var0.equals("1.6") ? (!var0.equals("7") && !var0.equals("1.7") ? 0 : 3342336) : 3276800) : 3211264)))) : 2949123;
    }

    public static String externalClassVersion(int var0) {
        switch (var0) {
            case 2949123:
                return "1.0";
            case 3014656:
                return "1.2";
            case 3080192:
                return "1.3";
            case 3145728:
                return "1.4";
            case 3211264:
                return "1.5";
            case 3276800:
                return "1.6";
            case 3342336:
                return "1.7";
            default:
                return null;
        }
    }

    public static void checkVersionNumbers(int var0) throws UnsupportedOperationException {
        if (var0 < 2949123 || var0 > 3342336) {
            throw new UnsupportedOperationException("Unsupported class version number [" + internalMajorClassVersion(var0) + "." + internalMinorClassVersion(var0) + "] (maximum " + 51 + "." + 0 + ", Java " + "1.7" + ")");
        }
    }

    public static String internalClassName(String var0) {
        return var0.replace('.', '/');
    }

    public static String externalFullClassDescription(int var0, String var1) {
        return externalClassAccessFlags(var0) + externalClassName(var1);
    }

    public static String externalClassName(String var0) {
        return var0.replace('/', '.');
    }

    public static String externalBaseType(String var0) {
        int var1 = var0.indexOf("[]");
        return var1 >= 0 ? var0.substring(0, var1) : var0;
    }

    public static String externalShortClassName(String var0) {
        int var1 = var0.lastIndexOf(46);
        return var0.substring(var1 + 1);
    }

    public static boolean isInternalArrayType(String var0) {
        return var0.length() > 1 && var0.charAt(0) == '[';
    }

    public static int internalArrayTypeDimensionCount(String var0) {
        int var1;
        for (var1 = 0; var0.charAt(var1) == '['; ++var1) {
        }

        return var1;
    }

    public static boolean isInternalArrayInterfaceName(String var0) {
        return "java/lang/Object".equals(var0) || "java/lang/Cloneable".equals(var0) || "java/io/Serializable".equals(var0);
    }

    public static boolean isInternalPrimitiveType(char var0) {
        return var0 == 'Z' || var0 == 'B' || var0 == 'C' || var0 == 'S' || var0 == 'I' || var0 == 'F' || var0 == 'J' || var0 == 'D';
    }

    public static boolean isInternalCategory2Type(String var0) {
        return var0.length() == 1 && (var0.charAt(0) == 'J' || var0.charAt(0) == 'D');
    }

    public static boolean isInternalClassType(String var0) {
        int var1 = var0.length();
        return var1 > 1 && var0.charAt(var1 - 1) == ';';
    }

    public static String internalTypeFromClassName(String var0) {
        return internalArrayTypeFromClassName(var0, 0);
    }

    public static String internalArrayTypeFromClassName(String var0, int var1) {
        StringBuffer var2 = new StringBuffer(var0.length() + var1 + 2);

        for (int var3 = 0; var3 < var1; ++var3) {
            var2.append('[');
        }

        return var2.append('L').append(var0).append(';').toString();
    }

    public static String internalTypeFromArrayType(String var0) {
        int var1 = var0.lastIndexOf(91);
        return var0.substring(var1 + 1);
    }

    public static String internalClassNameFromClassType(String var0) {
        return isInternalClassType(var0) ? var0.substring(var0.indexOf(76) + 1, var0.length() - 1) : var0;
    }

    public static String internalClassNameFromType(String var0) {
        if (!isInternalClassType(var0)) {
            return null;
        } else {
            if (isInternalArrayType(var0)) {
                var0 = internalTypeFromArrayType(var0);
            }

            return internalClassNameFromClassType(var0);
        }
    }

    public static boolean isInitializer(String var0) {
        return var0.equals("<clinit>") || var0.equals("<init>");
    }

    public static String internalMethodReturnType(String var0) {
        int var1 = var0.indexOf(41);
        return var0.substring(var1 + 1);
    }


    public static int internalTypeSize(String var0) {
        if (var0.length() == 1) {
            char var1 = var0.charAt(0);
            if (var1 == 'J' || var1 == 'D') {
                return 2;
            }

            if (var1 == 'V') {
                return 0;
            }
        }

        return 1;
    }

    public static String internalType(String var0) {
        int var1 = externalArrayTypeDimensionCount(var0);
        if (var1 > 0) {
            var0 = var0.substring(0, var0.length() - var1 * "[]".length());
        }

        int var2 = var0.equals("void") ? 86 : (var0.equals("boolean") ? 90 : (var0.equals("byte") ? 66 : (var0.equals("char") ? 67 : (var0.equals("short") ? 83 : (var0.equals("int") ? 73 : (var0.equals("float") ? 70 : (var0.equals("long") ? 74 : (var0.equals("double") ? 68 : (var0.equals("%") ? 37 : 0)))))))));
        String var3 = var2 != 0 ? String.valueOf((char) var2) : 'L' + internalClassName(var0) + ';';

        for (int var4 = 0; var4 < var1; ++var4) {
            var3 = '[' + var3;
        }

        return var3;
    }

    public static int externalArrayTypeDimensionCount(String var0) {
        int var1 = 0;
        int var2 = "[]".length();

        for (int var3 = var0.length() - var2; var0.regionMatches(var3, "[]", 0, var2); var3 -= var2) {
            ++var1;
        }

        return var1;
    }

    public static String externalType(String var0) {
        int var1 = internalArrayTypeDimensionCount(var0);
        if (var1 > 0) {
            var0 = var0.substring(var1);
        }

        char var2 = var0.charAt(0);
        String var3 = var2 == 'V' ? "void" : (var2 == 'Z' ? "boolean" : (var2 == 'B' ? "byte" : (var2 == 'C' ? "char" : (var2 == 'S' ? "short" : (var2 == 'I' ? "int" : (var2 == 'F' ? "float" : (var2 == 'J' ? "long" : (var2 == 'D' ? "double" : (var2 == '%' ? "%" : (var2 == 'L' ? externalClassName(var0.substring(1, var0.indexOf(59))) : null))))))))));
        if (var3 == null) {
            throw new IllegalArgumentException("Unknown type [" + var0 + "]");
        } else {
            for (int var4 = 0; var4 < var1; ++var4) {
                var3 = var3 + "[]";
            }

            return var3;
        }
    }

    public static boolean isInternalMethodDescriptor(String var0) {
        return var0.charAt(0) == '(';
    }

    public static boolean isExternalMethodNameAndArguments(String var0) {
        return var0.indexOf(40) > 0;
    }


    public static String internalMethodDescriptor(String var0, List var1) {
        StringBuffer var2 = new StringBuffer();
        var2.append('(');

        for (int var3 = 0; var3 < var1.size(); ++var3) {
            var2.append(internalType((String) var1.get(var3)));
        }

        var2.append(')');
        var2.append(internalType(var0));
        return var2.toString();
    }

    public static String externalFullFieldDescription(int var0, String var1, String var2) {
        return externalFieldAccessFlags(var0) + externalType(var2) + ' ' + var1;
    }


    public static String externalClassAccessFlags(int var0) {
        return externalClassAccessFlags(var0, "");
    }

    public static String externalClassAccessFlags(int var0, String var1) {
        if (var0 == 0) {
            return "";
        } else {
            StringBuffer var2 = new StringBuffer(50);
            if ((var0 & 1) != 0) {
                var2.append(var1).append("public").append(' ');
            }

            if ((var0 & 2) != 0) {
                var2.append(var1).append("private").append(' ');
            }

            if ((var0 & 4) != 0) {
                var2.append(var1).append("protected").append(' ');
            }

            if ((var0 & 8) != 0) {
                var2.append(var1).append("static").append(' ');
            }

            if ((var0 & 16) != 0) {
                var2.append(var1).append("final").append(' ');
            }

            if ((var0 & 8192) != 0) {
                var2.append(var1).append("@");
            }

            if ((var0 & 512) != 0) {
                var2.append(var1).append("interface").append(' ');
            } else if ((var0 & 16384) != 0) {
                var2.append(var1).append("enum").append(' ');
            } else if ((var0 & 1024) != 0) {
                var2.append(var1).append("abstract").append(' ');
            } else if ((var0 & 4096) != 0) {
                var2.append(var1).append("synthetic").append(' ');
            }

            return var2.toString();
        }
    }

    public static String externalFieldAccessFlags(int var0) {
        return externalFieldAccessFlags(var0, "");
    }

    public static String externalFieldAccessFlags(int var0, String var1) {
        if (var0 == 0) {
            return "";
        } else {
            StringBuffer var2 = new StringBuffer(50);
            if ((var0 & 1) != 0) {
                var2.append(var1).append("public").append(' ');
            }

            if ((var0 & 2) != 0) {
                var2.append(var1).append("private").append(' ');
            }

            if ((var0 & 4) != 0) {
                var2.append(var1).append("protected").append(' ');
            }

            if ((var0 & 8) != 0) {
                var2.append(var1).append("static").append(' ');
            }

            if ((var0 & 16) != 0) {
                var2.append(var1).append("final").append(' ');
            }

            if ((var0 & 64) != 0) {
                var2.append(var1).append("volatile").append(' ');
            }

            if ((var0 & 128) != 0) {
                var2.append(var1).append("transient").append(' ');
            }

            if ((var0 & 4096) != 0) {
                var2.append(var1).append("synthetic").append(' ');
            }

            return var2.toString();
        }
    }

    public static String externalMethodAccessFlags(int var0) {
        return externalMethodAccessFlags(var0, "");
    }

    public static String externalMethodAccessFlags(int var0, String var1) {
        if (var0 == 0) {
            return "";
        } else {
            StringBuffer var2 = new StringBuffer(50);
            if ((var0 & 1) != 0) {
                var2.append(var1).append("public").append(' ');
            }

            if ((var0 & 2) != 0) {
                var2.append(var1).append("private").append(' ');
            }

            if ((var0 & 4) != 0) {
                var2.append(var1).append("protected").append(' ');
            }

            if ((var0 & 8) != 0) {
                var2.append(var1).append("static").append(' ');
            }

            if ((var0 & 16) != 0) {
                var2.append(var1).append("final").append(' ');
            }

            if ((var0 & 32) != 0) {
                var2.append(var1).append("synchronized").append(' ');
            }

            if ((var0 & 64) != 0) {
                var2.append(var1).append("bridge").append(' ');
            }

            if ((var0 & 128) != 0) {
                var2.append(var1).append("varargs").append(' ');
            }

            if ((var0 & 256) != 0) {
                var2.append(var1).append("native").append(' ');
            }

            if ((var0 & 1024) != 0) {
                var2.append(var1).append("abstract").append(' ');
            }

            if ((var0 & 2048) != 0) {
                var2.append(var1).append("strictfp").append(' ');
            }

            if ((var0 & 4096) != 0) {
                var2.append(var1).append("synthetic").append(' ');
            }

            return var2.toString();
        }
    }

    public static String externalMethodReturnType(String var0) {
        return externalType(internalMethodReturnType(var0));
    }

    private static String externalMethodReturnTypeAndName(String var0, String var1, String var2) {
        return var1.equals("<init>") ? externalShortClassName(externalClassName(var0)) : externalMethodReturnType(var2) + ' ' + var1;
    }


    public static String internalPackageName(String var0) {
        String var1 = internalPackagePrefix(var0);
        int var2 = var1.length();
        return var2 > 0 ? var1.substring(0, var2 - 1) : "";
    }

    public static String internalPackagePrefix(String var0) {
        return var0.substring(0, var0.lastIndexOf(47, var0.length() - 2) + 1);
    }

    public static String externalPackageName(String var0) {
        String var1 = externalPackagePrefix(var0);
        int var2 = var1.length();
        return var2 > 0 ? var1.substring(0, var2 - 1) : "";
    }

    public static String externalPackagePrefix(String var0) {
        return var0.substring(0, var0.lastIndexOf(46, var0.length() - 2) + 1);
    }
}